from tkinter import *
from PIL import Image, ImageTk
import numpy as np
import cv2
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
import tkinter as tk
import threading

emotion_model = Sequential()

emotion_model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(48, 48, 1)))
emotion_model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
emotion_model.add(MaxPooling2D(pool_size=(2, 2)))
emotion_model.add(Dropout(0.25))

emotion_model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
emotion_model.add(MaxPooling2D(pool_size=(2, 2)))
emotion_model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
emotion_model.add(MaxPooling2D(pool_size=(2, 2)))
emotion_model.add(Dropout(0.25))

emotion_model.add(Flatten())
emotion_model.add(Dense(1024, activation='relu'))
emotion_model.add(Dropout(0.5))
emotion_model.add(Dense(7, activation='softmax'))
emotion_model.load_weights('emotion_model.h5')

cv2.ocl.setUseOpenCL(False)

emotion_dict = {0: "   Angry   ", 1: "Disgusted", 2: "  Fearful  ", 3: "   Happy   ", 4: "  Neutral  ",
                5: "    Sad    ", 6: "Surprised"}

emoji_dist_panda = {0: r"D:\Machine learning project\Python creat emoji\emoji\panda\angry_panda.jpg",
              1: r"D:\Machine learning project\Python creat emoji\emoji\panda\disgusted_panda.jpg",
              2: r"D:\Machine learning project\Python creat emoji\emoji\panda\fearful_panda.jpg",
              3: r"D:\Machine learning project\Python creat emoji\emoji\panda\happy_panda.jpg",
              4: r"D:\Machine learning project\Python creat emoji\emoji\panda\netural_panda.jpg",
              5: r"D:\Machine learning project\Python creat emoji\emoji\panda\sad_panda.jpg",
              6: r"D:\Machine learning project\Python creat emoji\emoji\panda\surprised_panda.jpg"}

emoji_dist_kun = {0: r"D:\Machine learning project\Python creat emoji\emoji\kun\angry_kun.jpg",
              1: r"D:\Machine learning project\Python creat emoji\emoji\kun\disgusted_kun.jpg",
              2: r"D:\Machine learning project\Python creat emoji\emoji\kun\fearful_kun.jpg",
              3: r"D:\Machine learning project\Python creat emoji\emoji\kun\happy_kun.jpg",
              4: r"D:\Machine learning project\Python creat emoji\emoji\kun\netural_kun.jpg",
              5: r"D:\Machine learning project\Python creat emoji\emoji\kun\sad_kun.jpg",
              6: r"D:\Machine learning project\Python creat emoji\emoji\kun\surprised_kun.jpg"}

emoji_dist_bone = {0: r"D:\Machine learning project\Python creat emoji\emoji\bone\angry_bone.jpg",
              1: r"D:\Machine learning project\Python creat emoji\emoji\bone\disgusted_bone.jpg",
              2: r"D:\Machine learning project\Python creat emoji\emoji\bone\fearful_bone.jpg",
              3: r"D:\Machine learning project\Python creat emoji\emoji\bone\happy_bone.jpg",
              4: r"D:\Machine learning project\Python creat emoji\emoji\bone\netural_bone.jpg",
              5: r"D:\Machine learning project\Python creat emoji\emoji\bone\sad_bone.jpg",
              6: r"D:\Machine learning project\Python creat emoji\emoji\bone\surprised_bone.jpg"}

emoji_dist_monkey = {0: r"D:\Machine learning project\Python creat emoji\emoji\monkey\angry_monkey.jpg",
              1: r"D:\Machine learning project\Python creat emoji\emoji\monkey\disgusted_monkey.jpg",
              2: r"D:\Machine learning project\Python creat emoji\emoji\monkey\fearful_monkey.jpg",
              3: r"D:\Machine learning project\Python creat emoji\emoji\monkey\happy_monkey.jpg",
              4: r"D:\Machine learning project\Python creat emoji\emoji\monkey\netural_monkey.jpg",
              5: r"D:\Machine learning project\Python creat emoji\emoji\monkey\sad_monkey.jpg",
              6: r"D:\Machine learning project\Python creat emoji\emoji\monkey\surprised_monkey.jpg"}

emoji_dist_pig = {0: r"D:\Machine learning project\Python creat emoji\emoji\pig\angry_pig.jpg",
              1: r"D:\Machine learning project\Python creat emoji\emoji\pig\disgusted_pig.jpg",
              2: r"D:\Machine learning project\Python creat emoji\emoji\pig\fearful_pig.jpg",
              3: r"D:\Machine learning project\Python creat emoji\emoji\pig\happy_pig.jpg",
              4: r"D:\Machine learning project\Python creat emoji\emoji\pig\netural_pig.jpg",
              5: r"D:\Machine learning project\Python creat emoji\emoji\pig\sad_pig.jpg",
              6: r"D:\Machine learning project\Python creat emoji\emoji\pig\surprised_pig.jpg"}

emoji_dist_shit = {0: r"D:\Machine learning project\Python creat emoji\emoji\shit\angry_shit.jpg",
              1: r"D:\Machine learning project\Python creat emoji\emoji\shit\disgusted_shit.jpg",
              2: r"D:\Machine learning project\Python creat emoji\emoji\shit\fearful_shit.jpg",
              3: r"D:\Machine learning project\Python creat emoji\emoji\shit\happy_shit.jpg",
              4: r"D:\Machine learning project\Python creat emoji\emoji\shit\netural_shit.jpg",
              5: r"D:\Machine learning project\Python creat emoji\emoji\shit\sad_shit.jpg",
              6: r"D:\Machine learning project\Python creat emoji\emoji\shit\surprised_shit.jpg"}
emoji_dist_spirit = {0: r"D:\Machine learning project\Python creat emoji\emoji\spirit\angry_spirit.jpg",
              1: r"D:\Machine learning project\Python creat emoji\emoji\spirit\disgusted_spirit.jpg",
              2: r"D:\Machine learning project\Python creat emoji\emoji\spirit\fearful_spirit.jpg",
              3: r"D:\Machine learning project\Python creat emoji\emoji\spirit\happy_spirit.jpg",
              4: r"D:\Machine learning project\Python creat emoji\emoji\spirit\netural_spirit.jpg",
              5: r"D:\Machine learning project\Python creat emoji\emoji\spirit\sad_spirit.jpg",
              6: r"D:\Machine learning project\Python creat emoji\emoji\spirit\surprised_spirit.jpg"}
emoji_dist_wild_boar = {0: r"D:\Machine learning project\Python creat emoji\emoji\wild boar\angry_wild.jpg",
              1: r"D:\Machine learning project\Python creat emoji\emoji\wild boar\disgusted_wild.jpg",
              2: r"D:\Machine learning project\Python creat emoji\emoji\wild boar\fearful_wild.jpg",
              3: r"D:\Machine learning project\Python creat emoji\emoji\wild boar\happy_wild.jpg",
              4: r"D:\Machine learning project\Python creat emoji\emoji\wild boar\netural_wild.jpg",
              5: r"D:\Machine learning project\Python creat emoji\emoji\wild boar\sad_wild boar.jpg",
              6: r"D:\Machine learning project\Python creat emoji\emoji\wild boar\surprised_wild.jpg"}
emoji_dist_dinosaur = {0: r"D:\Machine learning project\Python creat emoji\emoji\dinosaur\angry_dinosaur.jpg",
              1: r"D:\Machine learning project\Python creat emoji\emoji\dinosaur\disgusted_dinosaur.jpg",
              2: r"D:\Machine learning project\Python creat emoji\emoji\dinosaur\fearful_dinosaur.jpg",
              3: r"D:\Machine learning project\Python creat emoji\emoji\dinosaur\happy_dinosaur.jpg",
              4: r"D:\Machine learning project\Python creat emoji\emoji\dinosaur\netural_dinosaur.jpg",
              5: r"D:\Machine learning project\Python creat emoji\emoji\dinosaur\sad_dinosaur.jpg",
              6: r"D:\Machine learning project\Python creat emoji\emoji\dinosaur\surprised_dinosaur.jpg"}
emoji_dist_Alien = {0: r"D:\Machine learning project\Python creat emoji\emoji\Alien\angry_Alien.jpg",
              1: r"D:\Machine learning project\Python creat emoji\emoji\Alien\disgusted_Alien.jpg",
              2: r"D:\Machine learning project\Python creat emoji\emoji\Alien\fearful.jpg",
              3: r"D:\Machine learning project\Python creat emoji\emoji\Alien\happy_Alien.jpg",
              4: r"D:\Machine learning project\Python creat emoji\emoji\Alien\netural__Alien.jpg",
              5: r"D:\Machine learning project\Python creat emoji\emoji\Alien\sad_Alien.jpg",
              6: r"D:\Machine learning project\Python creat emoji\emoji\Alien\surprised_Alien.jpg"}








global last_frame1
last_frame1 = np.zeros((480, 640, 3), dtype=np.uint8)
global cap1

show_text = [0]

class gui:
    value:None

    def __init__(self):
        self.value = emoji_dist_panda

    def xiugai(self,values):
        if values == 'panda':
            self.value = emoji_dist_panda
        elif values == 'kun':
            self.value = emoji_dist_kun
        elif values == 'bone':
            self.value = emoji_dist_bone
        elif values == 'monkey':
            self.value = emoji_dist_monkey
        elif values == 'pig':
            self.value = emoji_dist_pig
        elif values == 'Alien':
            self.value = emoji_dist_Alien
        elif values == 'dinosaur':
            self.value = emoji_dist_dinosaur
        elif values == 'shit':
            self.value = emoji_dist_shit
        elif values == 'spirit':
            self.value = emoji_dist_spirit
        elif values == 'wild boar':
            self.value = emoji_dist_wild_boar


    def show(self):
        cap = cv2.VideoCapture(0)
        face_cascade = cv2.CascadeClassifier(
            r'D:\Machine learning project\Python creat emoji\emoji-creator-project-code\haarcascade_frontalface_default.xml')
        while True:
            ret, frame1 = cap.read()
            gray_frame = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
            num_faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.3, minNeighbors=5)

            for (x, y, w, h) in num_faces:
                cv2.rectangle(frame1, (x, y - 50), (x + w, y + h + 10), (255, 0, 0), 2)
                roi_gray_frame = gray_frame[y:y + h, x:x + w]
                cropped_img = np.expand_dims(np.expand_dims(cv2.resize(roi_gray_frame, (48, 48)), -1), 0)
                prediction = emotion_model.predict(cropped_img)

                maxindex = int(np.argmax(prediction))
                show_text[0] = maxindex  # 更新展示的表情索引
                cv2.putText(frame1, emotion_dict[maxindex], (x + 20, y - 60), cv2.FONT_HERSHEY_SIMPLEX, 1,
                            (255, 255, 255),
                            2, cv2.LINE_AA)

            # 展示实时视频
            cv2.imshow('AI&EN', frame1)
            cv2.moveWindow('AI&EN', 50, 50)
            # 获取对应的表情图像并显示
            frame2 = cv2.imread(self.value[show_text[0]])
            cv2.imshow('AI_EN', frame2)
            cv2.moveWindow('AI_EN', 800, 50)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                cap.release()
                cv2.destroyAllWindows()
                break

def tkwindow(mygui):
    def button_click(value):
        mygui.xiugai(value)

    root = tk.Tk()

    # 创建按钮
    button1 = tk.Button(root, text="panda", command=lambda: button_click("panda"), width=10)
    button1.grid(row=0, column=0, padx=5, pady=5)

    button2 = tk.Button(root, text="monkey", command=lambda: button_click("monkey"), width=10)
    button2.grid(row=0, column=1, padx=5, pady=5)

    button3 = tk.Button(root, text="pig", command=lambda: button_click("pig"), width=10)
    button3.grid(row=0, column=2, padx=5, pady=5)

    button4 = tk.Button(root, text="bone", command=lambda: button_click("bone"), width=10)
    button4.grid(row=0, column=3, padx=5, pady=5)

    button5 = tk.Button(root, text="kun", command=lambda: button_click("kun"), width=10)
    button5.grid(row=0, column=4, padx=5, pady=5)

    button6 = tk.Button(root, text="Alien", command=lambda: button_click("Alien"), width=10)
    button6.grid(row=1, column=0, padx=5, pady=5)

    button7 = tk.Button(root, text="dinosaur", command=lambda: button_click("dinosaur"), width=10)
    button7.grid(row=1, column=1, padx=5, pady=5)

    button8 = tk.Button(root, text="shit", command=lambda: button_click("shit"), width=10)
    button8.grid(row=1, column=2, padx=5, pady=5)

    button9 = tk.Button(root, text="spirit", command=lambda: button_click("spirit"), width=10)
    button9.grid(row=1, column=3, padx=5, pady=5)

    button10 = tk.Button(root, text="wild boar", command=lambda: button_click("wild boar"), width=10)
    button10.grid(row=1, column=4, padx=5, pady=5)

    result = None

    root.geometry("500x100")

    root.geometry("+100+600")
    root.mainloop()

def initGUI(gui):
    mygui.show()

if __name__ == '__main__':
    mygui = gui()
    guia = threading.Thread(target=initGUI,args=(mygui,))
    guib = threading.Thread(target=tkwindow,args=(mygui,))
    guia.start()
    guib.start()


